package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Gift;
import cn.wolfcode.crm.domain.GiftBill;
import cn.wolfcode.crm.domain.MemberInfo;
import cn.wolfcode.crm.mapper.GiftBillMapper;
import cn.wolfcode.crm.mapper.GiftMapper;
import cn.wolfcode.crm.mapper.MemberInfoMapper;
import cn.wolfcode.crm.qo.EmployeeQueryObject;
import cn.wolfcode.crm.qo.GiftQueryObject;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.service.IGiftService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Service
public class GiftServiceImpl implements IGiftService {
    @Autowired
    private GiftMapper giftMapper;
    @Autowired
    private MemberInfoMapper memberInfoMapper;
    @Autowired
    private GiftBillMapper giftBillMapper;

    @Override
    public void saveOrUpdate(Gift entity) {
        if (entity.getId() != null) {
            giftMapper.updateByPrimaryKey(entity);
        }else {
            giftMapper.insert(entity);
        }
    }

    @Override
    public void delete(Long id) {
        giftMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Gift get(Long id) {
        return giftMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Gift> list() {
        return giftMapper.selectAll();
    }

    @Override
    public void exchangeGift(Gift gift, String cardNumber) {
        //通过会员卡号查询会员积分
        MemberInfo vip = memberInfoMapper.selectByCardNumber(cardNumber);
        if (!vip.isState()) {
            throw new RuntimeException("会员卡已失效,请联系服务台");
        }
        if (vip.getPoints().compareTo(gift.getIntegral().multiply(gift.getNumber()))<0) {
            throw new RuntimeException("积分不足");
        }
        BigDecimal amount = vip.getPoints().subtract(gift.getIntegral().multiply(gift.getNumber()));
        vip.setPoints(amount);
        Employee user = (Employee) SecurityUtils.getSubject().getPrincipal();
        //插入一条兑换明细
        GiftBill giftBill = new GiftBill();
        giftBill.setGiftId(gift.getId());
        giftBill.setGiftName(gift.getName());
        giftBill.setInputId(user.getId());
        giftBill.setInputName(user.getUsername());
        giftBill.setInputTime(new Date());
        giftBill.setMemberId(vip.getId());
        giftBill.setMemberName(vip.getName());
        giftBill.setNumber(gift.getNumber());
        giftBill.setPoints(gift.getIntegral());
        giftBill.setTotalPoints(gift.getIntegral().multiply(gift.getNumber()));
        giftBillMapper.insert(giftBill);

    }



    @Override
    public PageResult query(GiftQueryObject qo) {
        int count = giftMapper.queryForCount(qo);
        if (count == 0) {
            return PageResult.EMPTY_RESULT;
        }
        List<Gift> list = giftMapper.queryForList(qo);
        return new PageResult(count,list);
    }
}
